/*
 * Created on Jun 23, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.peterrussell.contractionTimer.view;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

import org.peterrussell.contractionTimer.controller.Controller;
import org.peterrussell.contractionTimer.model.ContractionTimeDTO;

/**
 * @author peterr
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class MainCanvas extends Canvas {

	/**
	 * 
	 */
	
	private boolean isPainting = false;
	private long contractionTime;
	private long betweenContractionTime;
	private ContractionTimeDTO[] timeArray = new ContractionTimeDTO[0];
	private int currentTimer;
	public Command exitCommand;
	public Command startCommand;
	public Command stopCommand;
	public Command helpCommand;
	
	// Device dependent data
	private final int FONT_STANDARD = 0;
	private int maxRows = 6;
	private int maxCols = 5;
	private int fontSize = FONT_STANDARD;
	
	private Font standardFont;
	private Font currentTimerFont;
	private final int standardColor = 0;
	private final int currentTimerColor = 0;
	private final int dormantTimerColor = 8821951;

	private Font contractionFont;
	private Font intervalFont;
	private int contractionColor;
	private int intervalColor;
	private final int bgColor = 13434879;
	private final int lineColor = 8821951;
	private final int fillColor = 15526071;
	
	public MainCanvas() {
        //helpCommand = new Command("Help", Command.HELP, 2);
        exitCommand = new Command("Exit", Command.EXIT, 1);
        startCommand = new Command("Start", Command.ITEM, 2);
        stopCommand = new Command("Stop", Command.ITEM, 2);
        //ticker = new Ticker("Don't forget to have your bags packed in plenty of time.");
        this.addCommand(exitCommand);
        this.addCommand(startCommand);
        //canvas.addCommand(helpCommand);
        this.setCommandListener(new MainCanvasListener());
        //form.setTicker(ticker);
        setDeviceDependent();
        setFonts();

	}

	/* (non-Javadoc)
	 * @see javax.microedition.lcdui.Displayable#paint(javax.microedition.lcdui.Graphics)
	 */
	 public void paint (Graphics g) {
	 	if (isPainting == true){
	 		return;
	 	}
	 	isPainting = true;
	    g.setGrayScale (255);
	    g.setColor(bgColor);
	    g.fillRect (0, 0, getWidth(), getHeight());
	    g.setGrayScale (0);
	    
	    String contractionStr = "Contraction: "+UIHelper.dateToString(contractionTime);
	    int strWidth = contractionFont.stringWidth(contractionStr);
	    g.setColor(fillColor);
	    g.fillRoundRect(5, 7, strWidth+10, 21+contractionFont.getHeight(), 15, 15);
	    g.setColor(lineColor);
	    //g.drawLine(10, 7, 10+contractionFont.stringWidth("Contraction Timer: "+UIHelper.dateToString(contractionTime)), 7);
	    //g.drawLine(10, 28, 10+contractionFont.stringWidth("Contraction Timer: "+UIHelper.dateToString(contractionTime)), 28);
	    g.drawRoundRect(5, 7, strWidth+10, 21+contractionFont.getHeight(), 15, 15);
	    g.drawRoundRect(6, 8, strWidth+8, 19+contractionFont.getHeight(), 15, 15);
	    g.setColor(contractionColor);
	    g.setFont(contractionFont);
	    g.drawString(contractionStr,10,10,Graphics.TOP|Graphics.LEFT);
	    
	    g.setColor(intervalColor);
	    g.setFont(intervalFont);
	    g.drawString("Interval: "+UIHelper.dateToString(betweenContractionTime),10,25,Graphics.TOP|Graphics.LEFT);
	    g.setFont(standardFont);
	    g.setColor(standardColor);

	    String colString = "";
//	    strWidth = standardFont.stringWidth(" #  ##:##:##  ##:##:##   ##:##:##  ##:##:## (#)");
	    if (maxCols == 4) {
	        strWidth = standardFont.stringWidth("##:##  ##:##   ##:##  ##:## (#)");
	        colString = "Cont'n   Avg     Int'val   Avg ";
	    } else if (maxCols == 3) {
	    	strWidth = standardFont.stringWidth("##:##  ##:##   ##:## (#)");
	    	colString = "Cont'n   Int'val   Avg ";
	    } else if (maxCols == 2) {
	    	strWidth = standardFont.stringWidth("##:##  ##:## (#)");
	    	colString = "Int'val   Avg ";
	    }
	    int numRows = timeArray.length <= maxRows?timeArray.length:maxRows;
	    g.setColor(fillColor);
	    g.fillRoundRect(5, 47, strWidth+10, 20+numRows*15, 15, 15);
	    g.setColor(lineColor);
	    //g.drawLine(10, 7, 10+contractionFont.stringWidth("Contraction Timer: "+UIHelper.dateToString(contractionTime)), 7);
	    //g.drawLine(10, 28, 10+contractionFont.stringWidth("Contraction Timer: "+UIHelper.dateToString(contractionTime)), 28);
	    g.drawRoundRect(5, 47, strWidth+10, 20+numRows*15, 15, 15);
	    g.drawRoundRect(6, 48, strWidth+8, 20+numRows*15, 15, 15);
	    g.setFont(standardFont);
	    g.setColor(standardColor);
	    g.drawString(colString,10,50,Graphics.TOP|Graphics.LEFT);
	    
	    int gap = 0;
	    for (int i = timeArray.length-1; i >= timeArray.length-numRows ; i--) {
	    	ContractionTimeDTO thisDTO = timeArray[i];
		    if (maxCols == 4) {
		    	g.drawString(UIHelper.dateToString(thisDTO.getContractionTime())+"  "+UIHelper.dateToString(thisDTO.getContractionTimeAvg())+"  "+UIHelper.dateToString(thisDTO.getBetweenContractionTime())+"  "+UIHelper.dateToString(thisDTO.getBetweenContractionTimeAvg())+" ("+thisDTO.getContractionTimeAvgOf()+")",10,65+gap,Graphics.TOP|Graphics.LEFT);
		    } else if (maxCols == 3) {
		    	g.drawString(UIHelper.dateToString(thisDTO.getContractionTime())+"  "+UIHelper.dateToString(thisDTO.getBetweenContractionTime())+"  "+UIHelper.dateToString(thisDTO.getBetweenContractionTimeAvg())+" ("+thisDTO.getContractionTimeAvgOf()+")",10,65+gap,Graphics.TOP|Graphics.LEFT);
		    } else if (maxCols == 2) {
		    	g.drawString(UIHelper.dateToString(thisDTO.getBetweenContractionTime())+"  "+UIHelper.dateToString(thisDTO.getBetweenContractionTimeAvg())+" ("+thisDTO.getContractionTimeAvgOf()+")",10,65+gap,Graphics.TOP|Graphics.LEFT);
		    }
			gap +=15;
		}
	    isPainting = false;
	  }

	 private void setFonts() {
	 	standardFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL);
	 	currentTimerFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL);
	 	standardFont = Font.getDefaultFont();
	 	currentTimerFont = Font.getDefaultFont();
	 	contractionFont = standardFont;
	 	intervalFont = standardFont;
	 	contractionColor = standardColor;
	 	intervalColor = standardColor;
}
	/**
	 * @return Returns the betweenContactionTime.
	 */
	public long getBetweenContractionTime() {
		return betweenContractionTime;
	}
	/**
	 * @param betweenContactionTime The betweenContactionTime to set.
	 */
	public void setBetweenContractionTime(long betweenContactionTime) {
		this.betweenContractionTime = betweenContactionTime;
	}
	/**
	 * @return Returns the contactionTime.
	 */
	public long getContractionTime() {
		return contractionTime;
	}
	/**
	 * @param contactionTime The contactionTime to set.
	 */
	public void setContractionTime(long contactionTime) {
		this.contractionTime = contactionTime;
	}
	/**
	 * @return Returns the timeArray.
	 */
	public ContractionTimeDTO[] getTimeArray() {
		return timeArray;
	}
	/**
	 * @param timeArray The timeArray to set.
	 */
	public void setTimeArray(ContractionTimeDTO[] timeArray) {
		this.timeArray = timeArray;
	}
	/**
	 * @return Returns the currentTimer.
	 */
	public int getCurrentTimer() {
		return currentTimer;
	}
	/**
	 * @param currentTimer The currentTimer to set.
	 */
	public void setCurrentTimer(int currentTimer) {
		this.currentTimer = currentTimer;
	 	contractionFont = standardFont;
	 	intervalFont = standardFont;
	 	contractionColor = standardColor;
	 	intervalColor = standardColor;
		if (currentTimer == Controller.BETWEEN_CONTRACTION) {
			intervalFont = currentTimerFont;
			intervalColor = currentTimerColor;
		 	contractionFont = standardFont;
		 	contractionColor = dormantTimerColor;
		} else if (currentTimer == Controller.CONTRACTION) {
			contractionFont = currentTimerFont;
			contractionColor = currentTimerColor;
			intervalFont = standardFont;
			intervalColor = dormantTimerColor;
		}
	}

	
	private void setDeviceDependent(){
		int ySize = this.getHeight();
		int xSize = this.getWidth();
		
		// How many columns can we fit on display?
		standardFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL);
		int col4Width = standardFont.stringWidth("  ##:##  ##:##   ##:##  ##:## (#)  ");
		int col3Width = standardFont.stringWidth("  ##:##  ##:##   ##:## (#)  ");
		int col2Width = standardFont.stringWidth("  ##:##  ##:##  (#)  ");
		System.out.println("ySize:"+ySize);
		System.out.println("xSize:"+xSize);
		System.out.println("col4Width:"+col4Width);
		if (xSize > col4Width) {
			maxCols = 4;
		} else if (xSize > col3Width) {
			maxCols = 3;
		} else if (xSize > col2Width) {
			maxCols = 2;
		} else {
			maxCols = 0;
		}
		
        // How many rows can we fit on display?
		int strHeight = standardFont.getHeight();
		maxRows = 0;
		for (int i = 80; i < ySize; i+=15) {
			maxRows++;
		}
	}
}
